﻿using System;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using Autodesk.Revit.DB;
using Microsoft.CSharp.RuntimeBinder;

namespace HYRevitCode.TransactionUtility
{
	public class RevitTransactionManger
	{
		public static bool DoTransaction(Document inputDoc, TransactionWorkDelegate useDelegate, object delegateUseValue, out Exception faceException, bool ifIsSubTransaction = false, string strUseTransactionName = null)
		{
			object arg = null;
			faceException = null;
			if (strUseTransactionName == null)
			{
				strUseTransactionName = RevitTransactionManger.m_strDefultTransactionName;
			}
			if (ifIsSubTransaction)
			{
				arg = new SubTransaction(inputDoc);
			}
			else
			{
				arg = new Transaction(inputDoc, strUseTransactionName);
			}
			bool result;
			try
			{
                //if (RevitTransactionManger.<>o__1.<>p__0 == null)
                //{
                //    RevitTransactionManger.<>o__1.<>p__0 = CallSite<Action<CallSite, object>>.Create(Binder.InvokeMember(CSharpBinderFlags.ResultDiscarded, "Start", null, typeof(RevitTransactionManger), new CSharpArgumentInfo[]
                //    {
                //        CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
                //    }));
                //}
                //RevitTransactionManger.<>o__1.<>p__0.Target(RevitTransactionManger.<>o__1.<>p__0, arg);
                //result = useDelegate(delegateUseValue);
			}
			catch (Exception ex)
			{
				faceException = ex;
                //if (RevitTransactionManger.<>o__1.<>p__2 == null)
                //{
                //    RevitTransactionManger.<>o__1.<>p__2 = CallSite<Func<CallSite, object, bool>>.Create(Binder.UnaryOperation(CSharpBinderFlags.None, ExpressionType.IsTrue, typeof(RevitTransactionManger), new CSharpArgumentInfo[]
                //    {
                //        CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
                //    }));
                //}
                //Func<CallSite, object, bool> target = RevitTransactionManger.<>o__1.<>p__2.Target;
                //CallSite <>p__ = RevitTransactionManger.<>o__1.<>p__2;
                //if (RevitTransactionManger.<>o__1.<>p__1 == null)
                //{
                //    RevitTransactionManger.<>o__1.<>p__1 = CallSite<Func<CallSite, object, object>>.Create(Binder.InvokeMember(CSharpBinderFlags.None, "HasStarted", null, typeof(RevitTransactionManger), new CSharpArgumentInfo[]
                //    {
                //        CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
                //    }));
                //}
                //if (target(<>p__, RevitTransactionManger.<>o__1.<>p__1.Target(RevitTransactionManger.<>o__1.<>p__1, arg)))
                //{
                //    if (RevitTransactionManger.<>o__1.<>p__3 == null)
                //    {
                //        RevitTransactionManger.<>o__1.<>p__3 = CallSite<Action<CallSite, object>>.Create(Binder.InvokeMember(CSharpBinderFlags.ResultDiscarded, "RollBack", null, typeof(RevitTransactionManger), new CSharpArgumentInfo[]
                //        {
                //            CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
                //        }));
                //    }
                //    RevitTransactionManger.<>o__1.<>p__3.Target(RevitTransactionManger.<>o__1.<>p__3, arg);
                //}
				result = false;
			}
			return false;
		}

		private static string m_strDefultTransactionName = "defultName";
	}
}
